<template>
  <div id="search">
    <!--这个头部是组件来的 S-->
    <Header :isFixed="true" childrenTitle="搜索"></Header>
    <!--这个头部是组件来的 E-->
    <div class="search-box">
      <i class="iconfont icon-sousuo search-box-icon"></i>
      <input
        type="text"
        name
        placeholder="请输入内容"
        class="search-btn"
        @keyup.enter="onSearch()"
        v-model="keyWords"
      >
      <div slot="action" @click="onSearch()" class="search-click">搜索</div>
    </div>
    <div class="search-history">
      <div class="search-history-title">
        <span class="search-history-text">搜索历史</span>
        <i class="iconfont search-history-icon" @click="onClean">&#xe692;</i>
      </div>
      <template v-if="searchHistoryList.length">
        <van-button
          size="normal"
          class="history-info history-info-btn"
          v-for="(item,index) in searchHistoryList"
          :key="index"
          @click="onSearch(item)"
        >{{item}}
        </van-button>
      </template>
      <p v-else class="search-history-tips">暂无历史记录</p>
    </div>
    <div class="fixed-btn-box">
      <van-button @click="$popRoute">关闭</van-button>
    </div>
  </div>
</template>

<script>
  import {mapActions, mapState, mapGetters, mapMutations} from "vuex";
  import {Search, Button, Dialog} from "vant";
  import Header from "@/components/header";
  /*  searchType  2019-1-17修改
   * 0 代表黄页
   */

  export default {
    name: "Search",
    props: ["source", "searchTypes"],
    components: {Header},
    data() {
      return {
        keyWords: "",
        searchType: null
      };
    },
    methods: {
      ...mapMutations("common/search", [
        "setKeyWordsToHistory",
        "cleanHistoryKeyWords"
      ]),
      async onSearch(text) {
        const keyWords = text || this.keyWords.trim();
        this.setKeyWordsToHistory(keyWords);
        this.$router.push({
          path: "/searchList",
          query: {searchType: this.searchType, keyWords}
        });
      },
      onClean() {
        this.cleanHistoryKeyWords();
        Dialog.alert({
          title: "提示",
          message: "清除成功！",
          className: "dialog-title"
        });
      }
    },
    computed: {
      ...mapState("common/search", {
        searchHistoryList: state => state.searchHistoryList
      }),
      ...mapState({
        platform: state => state.platform
      })
    },
    created() {
      this.searchType = this.$route.query["searchType"];
      document.title = "搜索 - " + this.platform.name;
    }
  };
</script>
<style lang="scss" scoped>
  @import "../../assets/common";

  #search {
    padding: 120px 30px 30px;
    background-color: #fff;
    min-height: 100vh;

    .search-box {
      box-sizing: border-box;
      border-radius: 40px;
      display: flex;
      justify-content: space-between;
      position: relative;

      .search-box-icon {
        top: 50%;
        transform: translateY(-50%);
        position: absolute;
        left: 16px;
        color: rgb(153, 153, 153);
        border: none;
      }
    }

    .search-btn {
      flex: 1;
      padding: 0px 60px;
      height: 64px;
      background-color: #eee;
      color: rgb(153, 153, 153);
      border-radius: 8px;
      border-width: 0px;
      overflow: hidden;
      @include alignCenter;

      &::-webkit-input-placeholder {
        line-height: 1.5;
      }
    }

    .search-click {
      display: inline-block;
      font-size: 30px;
      margin-left: 24px;
      min-width: 60px;
      line-height: 64px;
    }

    .search-history {
      margin-top: 33px;

      .search-history-title,
      .search-history-icon {
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;
      }

      .search-history-text {
        font-size: 26px;
        margin: 0px;
        color: rgb(153, 153, 153);
      }

      .search-history-tips {
        color: #a9a9a9;
        margin-top: 20px;
      }
    }

    .history-info {
      line-height: 64px;
      padding: 10px 20px;
      max-width: 100%;
      background-color: rgb(244, 244, 244);
      color: rgb(51, 51, 51);
      border-width: 0px;
      border-radius: 8px;
      margin: 16px 16px 0px 0px;
      @include t-overflow;
    }

    .hot-search {
      margin-top: 28px;
      font-size: 28px;
      color: rgb(153, 153, 153);
    }

    .hot-title {
      display: block;
      margin-bottom: 24px;
    }

    .dialog-title {
      .van-dialog__message {
        text-align: center;
      }
    }

    .fixed-btn-box {
      position: fixed;
      width: 96%;
      bottom: 0;
      left: 0;
      padding: 30px 2%;

      button {
        width: 100%;
        background: $themeColor;
        color: #fff;
        border-radius: 10px;
        font-size: 30px;
      }
    }
  }
</style>
